package org.geometerplus.fbreader.library;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.geometerplus.android.fbreader.RLog;
import org.geometerplus.android.fbreader.library.SQLiteBooksDatabase;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.image.ZLImage;
import org.geometerplus.zlibrary.core.image.ZLLoadableImage;
import org.zixuanstudio.ebook.R;
import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageData;
import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageManager;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;

public class ImageUtil {

	public static final HashMap<Long, Bitmap> mBookCoverCollection = new HashMap<Long, Bitmap>();
	public static final ArrayList<BookSpec> mBookCollection = new ArrayList<BookSpec>();

	public static ArrayList<Book> mBooks = new ArrayList<Book>();

	private static SyncImageLoader mSyncImageLoader;

	private static Context mContext;

	public static void initBooks(Context context) {
		mContext = context;
		if (SQLiteBooksDatabase.Instance() == null) {
			new SQLiteBooksDatabase(context, "load all book");
		}
		final BooksDatabase db = BooksDatabase.Instance();
		final FileInfoSet fileInfos = new FileInfoSet();
		final Map<Long, Book> savedBooksByFileId = db
				.loadBooks(fileInfos, true);
		final Map<Long, Book> savedBooksByBookId = new HashMap<Long, Book>();
		mBooks.clear();
		for (Book book : savedBooksByFileId.values()) {
			mBooks.add(book);
		}
	}

	public static void startLoadBookCover(final Context context,
			final Handler handler) {
		mBooks.clear();
		new Thread(new LoadBooksCover(context, handler)).start();
	}

	private static class LoadBooksCover implements Runnable {
		private Context context;
		private Handler handler;

		public LoadBooksCover(Context con, Handler hand) {
			context = con;
			handler = hand;
		}

		@Override
		public void run() {
			try {
				Looper.prepare();
				createBookCache(context);
				// Looper.loop();
			} catch (IOException e) {
				e.printStackTrace();
			}
			handler.sendEmptyMessage(0);
		}

	}

	private static void createBookCache(Context context) throws IOException {
		mSyncImageLoader = SyncImageLoader.getInstance();
		initBooks(context);
		mSyncImageLoader.createImageCache();
	}

	public static class BookSpec {
		public long bookId;
		public Bitmap bookCover;
		public String path;
	}

	public static Bitmap loadImageFromPath(String path) throws IOException {
		ZLFile zlFile = ZLFile.createFileByPath(path);
		ZLImage bookImage = Library.getCover(zlFile);
		final Book book = Book.getByFile(zlFile);
		if (bookImage instanceof ZLLoadableImage) {
			final ZLLoadableImage loadableImage = (ZLLoadableImage) bookImage;
			if (!loadableImage.isSynchronized()) {
				loadableImage.synchronize();
			}
		}
		final ZLAndroidImageData data = ((ZLAndroidImageManager) ZLAndroidImageManager
				.Instance()).getImageData(bookImage);
		Bitmap cover = data.getBitmap(120, 150);
		
		return cover;
	}

	public static Bitmap getDefaultBookCover() {
		final Resources resources = mContext.getResources();

		final Bitmap defaultCover = BitmapFactory.decodeResource(resources,
				R.drawable.cover_epub);
		return defaultCover;
	}
}
